package com.textcheck;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.dictionary.CustomDictionary;

/**
 * NLP相关配置类
 */
public class NlpConfig {
    
    // 同义词组映射
    private static final Map<String, Set<String>> SYNONYM_GROUPS = new HashMap<>();
    
    static {
        initializeNlp();
    }
    
    private static void initializeNlp() {
        try {
            // 添加专业术语
           // addTechnicalTerms();
            
            // 添加职业词汇
           // addProfessionTerms();
            
            // 添加同义词组
           // addSynonymGroups();
            
            // 设置HanLP配置
            HanLP.Config.ShowTermNature = true;
            
        } catch (Exception e) {
            System.err.println("NLP初始化失败: " + e.getMessage());
        }
    }
    
    /**
     * 添加技术相关术语
     */
    private static void addTechnicalTerms() {
        // 编程语言
        CustomDictionary.add("Java", "nz 1024");
        CustomDictionary.add("Python", "nz 1024");
        CustomDictionary.add("C++", "nz 1024");
        
        // 技术术语
        String[] terms = {
            "程序:n:1024",
            "软件:n:1024",
            "代码:n:1024",
            "应用:vn:1024",
            "应用程序:n:1024",
            "编程:vn:1024",
            "开发:vn:1024",
            "编写:v:1024",
            "研发:vn:1024",
            "基于:p:512"
        };
        
        for (String term : terms) {
            String[] parts = term.split(":");
            CustomDictionary.add(parts[0], parts[1] + " " + parts[2]);
        }
    }
    
    /**
     * 添加职业相关词汇
     */
    private static void addProfessionTerms() {
        String[] professions = {
            "工程师:n:1024",
            "程序员:n:1024",
            "开发者:n:1024",
            "研发人员:n:1024",
            "开发人员:n:1024",
            "软件工程师:n:1024",
            "开发工程师:n:1024"
        };
        
        for (String prof : professions) {
            String[] parts = prof.split(":");
            CustomDictionary.add(parts[0], parts[1] + " " + parts[2]);
            // 将职业名称添加到同义词组
            addToSynonymGroup("profession", parts[0]);
        }
    }
    
    /**
     * 添加同义词组
     */
    private static void addSynonymGroups() {
        // 添加动作同义词组
        addSynonymSet("action_develop", "开发", "研发", "编写", "制作", "设计");
        addSynonymSet("action_create", "建立", "创建", "创立", "成立", "设立");
        addSynonymSet("action_execute", "运行", "执行", "跑");
        
        // 添加程序相关同义词组
        addSynonymSet("software", "程序", "软件", "应用", "应用程序");
        addSynonymSet("code", "代码", "程序代码", "源代码");
        
        // 添加其他同义词组
        for (Map.Entry<String, Set<String>> entry : SYNONYM_GROUPS.entrySet()) {
            for (String word : entry.getValue()) {
                for (String synonym : entry.getValue()) {
                    if (!word.equals(synonym)) {
                        // 为每个词添加同义词信息
                        String mainPos = word.length() <= 2 ? "v 512" : "v 1024";
                        CustomDictionary.add(word, mainPos);
                    }
                }
            }
        }
    }
    
    /**
     * 添加同义词组
     */
    private static void addSynonymSet(String groupKey, String... words) {
        Set<String> synonyms = SYNONYM_GROUPS.computeIfAbsent(groupKey, k -> new HashSet<>());
        synonyms.addAll(Arrays.asList(words));
    }
    
    /**
     * 添加词到同义词组
     */
    private static void addToSynonymGroup(String groupKey, String word) {
        SYNONYM_GROUPS.computeIfAbsent(groupKey, k -> new HashSet<>()).add(word);
    }
    
    /**
     * 检查两个词是否为同义词
     */
    public static boolean areSynonyms(String word1, String word2) {
        if (word1.equals(word2)) {
            return true;
        }
        
        for (Set<String> group : SYNONYM_GROUPS.values()) {
            if (group.contains(word1) && group.contains(word2)) {
                return true;
            }
        }
        
        return false;
    }
}